'use strict';

/* Controllers */

angular.module('app')
    .controller('AppCtrl', ['$scope', '$translate', '$localStorage', '$window', '$rootScope', 'adminServices', 'catServices', 'messageServices', '$modal','workOrderService','$state','toaster',
        function ($scope, $translate, $localStorage, $window, $rootScope, adminServices, catServices, messageServices, $modal,workOrderService,$state,toaster) {
            // add 'ie' classes to html
            var isIE = !!navigator.userAgent.match(/MSIE/i);
            isIE && angular.element($window.document.body).addClass('ie');
            isSmartDevice($window) && angular.element($window.document.body).addClass('smart');
            // config
            $scope.app = {
                name: '管理系统',
                version: app.version,
                // for chart colors
                color: {
                    primary: '#7266ba',
                    info: '#23b7e5',
                    success: '#27c24c',
                    warning: '#fad733',
                    danger: '#f05050',
                    light: '#e8eff0',
                    dark: '#3a3f51',
                    black: '#1c2b36'
                },
                settings: {
                    themeID: 1,
                    navbarHeaderColor: 'bg-black',
                    navbarCollapseColor: 'bg-white-only',
                    asideColor: 'bg-black',
                    headerFixed: true,
                    asideFixed: false,
                    asideFolded: false,
                    asideDock: false,
                    container: false
                }
            }
            //版本历史
            $rootScope.versionList=app.versionList;
            // save settings to local storage
            if (angular.isDefined($localStorage.settings)) {
                $scope.app.settings = $localStorage.settings;
            } else {
                $localStorage.settings = $scope.app.settings;
            }
            $scope.$watch('app.settings', function () {
                if ($scope.app.settings.asideDock && $scope.app.settings.asideFixed) {
                    // aside dock and fixed must set the header fixed.
                    $scope.app.settings.headerFixed = true;
                }
                // save to local storage
                $localStorage.settings = $scope.app.settings;
            }, true);

            // angular translate
            $scope.lang = {isopen: false};
            $scope.langs = {en: 'English', de_DE: 'German', it_IT: 'Italian', cn: '中文'};
            $scope.selectLang = $scope.langs[$translate.proposedLanguage()] || "English";
            $scope.setLang = function (langKey, $event) {
                // set the current lang
                $scope.selectLang = $scope.langs[langKey];
                // You can change the language during runtime
                $translate.use(langKey);
                $scope.lang.isopen = !$scope.lang.isopen;
            };
            function isSmartDevice($window) {
                // Adapted from http://www.detectmobilebrowsers.com
                var ua = $window['navigator']['userAgent'] || $window['navigator']['vendor'] || $window['opera'];
                // Checks for iOs, Android, Blackberry, Opera Mini, and Windows mobile devices
                return (/iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/).test(ua);
            }


            //初始化数据
            $rootScope.initData = function () {


            }

            //token 超时和鉴权失败
            $rootScope.$on('userIntercepted', function (event, mag) {
                messageServices.openShowMessage("提示信息", "登录超时请重新登录", "access.signin");
            });
            //系统捕获异常
            $rootScope.$on('sysNetError', function (event, mag) {
                //messageServices.openShowMessage("系统异常信息", mag);
                toaster.pop('error', '系统异常信息', mag);
            });
            //http异常
            $rootScope.$on('netError', function (event, status, statusText) {
                //messageServices.openShowMessage("网络异常", "netError:" + status + "," + statusText);
                toaster.pop('error', '网络异常', "netError:" + status + "," + statusText);
            });
            //管理员密码修改
            $scope.adminPws = function () {
                var modalInstance = $modal.open({
                    templateUrl: 'tpl/op/pwdUpdate.html',
                    controller: 'adminPwsModalCtrl',
                    size: "sm"
                });
            };
            //打开版本信息
            $scope.openVersion= function() {
                var modalInstance = $modal.open({
                    templateUrl: 'tpl/blocks/cms_version.html',
                    size: "lg"
                });
            };

        }]);
//管理员更新密码
app.controller('adminPwsModalCtrl', ['$scope', 'adminServices', '$window', 'messageServices', '$modalInstance', '$timeout', '$rootScope','toolsService',
    function ($scope, adminServices, $window, messageServices, $modalInstance, $timeout, $rootScope,toolsService) {
        $scope.userPws = {"pws": "", "pws_new": "", "pws_new_confirm": ""};
        $scope.authError = "";
        function setTime() {
            $timeout(function () {
                $scope.authError = "";
            }, 2000);
        }
        $scope.saveUpdatePws = function (valid) {
            var pwd=toolsService.pwdAuthen($scope.userPws.pws_new)
            if ($scope.userPws.pws == "") {
                $scope.authError = "原密码必须输入";
                $window.document.getElementById("pws").focus();
                setTime();
            }
            else if (!pwd) {
                $scope.authError = "新密码必须是6至16位的数字、大小写字母组合";
                $window.document.getElementById("pws_new").focus();
                setTime();
            }
            else if ($scope.userPws.pws_new != $scope.userPws.pws_new_confirm) {
                $scope.authError = "新密码和确认密码不相同";
                $window.document.getElementById("pws_new").focus();
                setTime();

            } else {
                adminServices.updateAdminPws($rootScope.user.id,$scope.userPws.pws, $scope.userPws.pws_new).success(function (resultData) {
                    if (resultData.code==0) {
                        messageServices.openShowMessage("提示信息", "管理员密码更新成功").then(function (data) {
                            $modalInstance.close();
                        });
                    }else{
                        $scope.authError = "原密码不正确";
                        $window.document.getElementById("pws").focus();
                        setTime();
                    }
                });
            }
        }
        //取消按钮
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);